package ua.edu.nuos.tees.jdmmon.gui.charts;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import javax.swing.*;
import java.awt.*;

/**
 * Chart for three phase voltage/current
 *
 * @author Olha Mudrievska
 *         01.06.13
 */
public class ThreePhaseChart extends JFrame {

    public static final int SUBPLOT_COUNT = 3;
    private TimeSeriesCollection[] datasets;
    private long[] l = new long[3];

    public ThreePhaseChart(String title, String chartTitle) {
        super(title);

        final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new DateAxis("Time"));
        this.datasets = new TimeSeriesCollection[SUBPLOT_COUNT];

        for (int i = 0; i < SUBPLOT_COUNT; i++) {
            final TimeSeries series = new TimeSeries("L " + (i + 1), Millisecond.class);
            this.datasets[i] = new TimeSeriesCollection(series);
            final NumberAxis rangeAxis = new NumberAxis();
            rangeAxis.setRange(0.0, 499.0);
            final XYPlot subplot = new XYPlot(this.datasets[i], null, rangeAxis, new StandardXYItemRenderer());
            subplot.setBackgroundPaint(Color.lightGray);
            subplot.setDomainGridlinePaint(Color.white);
            subplot.setRangeGridlinePaint(Color.white);
            plot.add(subplot);
        }

        final JFreeChart chart = new JFreeChart(chartTitle, plot);
        chart.setBorderPaint(Color.black);
        chart.setBorderVisible(true);
        chart.setBackgroundPaint(Color.white);

        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        final ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        axis.setFixedAutoRange(60000.0);

        final JPanel content = new JPanel(new BorderLayout());

        final ChartPanel chartPanel = new ChartPanel(chart);
        content.add(chartPanel);

        chartPanel.setPreferredSize(new java.awt.Dimension(700, 520));
        chartPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setContentPane(content);
    }

    /**
     * Set phase L1 current/voltage
     * @param l1 value of L1 current/voltage
     */
    public void setL1(long l1) {
        this.l[0] = l1;
    }

    /**
     * Set phase L2 current*voltage
     * @param l2 value of L2 current/voltage
     */
    public void setL2(long l2) {
        this.l[1] = l2;
    }

    /**
     * Set phase L3 current*voltage
     * @param l3 value of L3 current/voltage
     */
    public void setL3(long l3) {
        this.l[2] = l3;
    }

    /**
     * Refresh all values
     */
    public void refreshChart() {
        for (int i = 0; i < SUBPLOT_COUNT; i++)
            datasets[i].getSeries(0).add(new Millisecond(), l[i]);
    }
}
